package com.woniu.malltinyone.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.malltinyone.common.CommonPage;
import com.woniu.malltinyone.common.CommonResult;
import com.woniu.malltinyone.mbg.model.PmsBrand;
import com.woniu.malltinyone.service.PmsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "PmsBrandController",description = "商品品牌管理")
@RestController
@RequestMapping("/brand")
@Slf4j
public class PmsBrandController {
    @Autowired
    private PmsBrandService brandService;
    @ApiOperation("获取所有品牌列表")
    @GetMapping(value = "/listAll")
    public CommonResult<List<PmsBrand>> getBrandList(){
        return CommonResult.success(brandService.listAllBrand());
    }
    @ApiOperation("添加品牌")
    @PostMapping(value = "/create")
    @ApiOperationSupport(ignoreParameters = {"id","productCount","productCommentCount"})
    public CommonResult createBrand(@RequestBody PmsBrand pmsBrand){
        CommonResult commonResult;
        int count = brandService.createBrand(pmsBrand);
        if(count==1){
            commonResult= CommonResult.success(pmsBrand);
            log.debug("createBrand success:{}",pmsBrand);
        }else {
            commonResult= CommonResult.failed("增加品牌失败");
            log.debug("createBrand failed:{}",pmsBrand);
        }
        return commonResult;
    }
    @ApiOperation("更新指定id的品牌信息")
    @PostMapping(value = "/update/{id}")
    public CommonResult updateBrand(@PathVariable("id") Long id,@RequestBody PmsBrand pmsBrandDto, BindingResult result){
        CommonResult commonResult;
        int count = brandService.updateBrand(id, pmsBrandDto);
        if(count==1){
            commonResult= CommonResult.success(pmsBrandDto);
            log.debug("updateBrand success:{}",pmsBrandDto);
        }else {
            commonResult=CommonResult.failed("更新品牌失败");
            log.debug("updateBrand failed:{}",pmsBrandDto);
        }
        return commonResult;
    }
    @ApiOperation("删除指定id的品牌信息")
    @GetMapping(value = "/delete/{id}")
    public CommonResult deleteBrand(@PathVariable("id") Long id){
        int count = brandService.deleteBrand(id);
        if(count==1){
            log.debug("deleteBrand success:{}",id);
            return CommonResult.success(null);
        }else {
            log.debug("deleteBrand failed:{}",id);
            return CommonResult.failed("删除品牌失败");
        }
    }
    @ApiOperation("分页查询品牌列表")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<PmsBrand>> listBrand(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize){
        List<PmsBrand> brandList = brandService.listBrand(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(brandList));
    }
    @ApiOperation("获取指定id的品牌详情")
    @GetMapping(value = "/{id}")
    public CommonResult<PmsBrand> brand(@PathVariable("id") Long id){
        return CommonResult.success(brandService.getBrand(id));
    }
}
